<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
?>


<script type="text/javascript" language="javascript">
    //    var profiledata = createStore(false, 'mprofiledata',['nama_sekolah','alamat_sekolah','url_logo'],'<?php echo base_url(); ?>' + 'profile/get_rows_combo');
    //    var imageStore=createStore(false, 'imgstore', [
    //        {name: 'name'},
    //        {name: 'url'},
    //        {name: 'size', type: 'float'},
    //        {name:'lastmod', type:'date', dateFormat:'timestamp'}
    //    ], '<?php echo base_url(); ?>' + 'profile/getImage');
    var imageStore=createStore(false, 'imgstore', [
        {name: 'caption'},
        {name: 'src'}        
    ], '<?php echo base_url(); ?>' + 'profile/getImage');
    
   
    var imageTpl = new Ext.XTemplate(
    '<tpl for=".">',
    '<div align="center" style="margin-bottom: 10px;" class="thumb-wrap">',
    '<img src="{src}" height="100" width="100"/>',
    '<br/><span >{caption}</span>',
    '</div>',
    '</tpl>'
);
    Ext.define('ImageViewField',{
        extend          : 'Ext.view.View',
        alias           : 'widget.imageviewfield',
        id              : 'imageviewfield_id',
        store: imageStore, //Ext.data.StoreManager.lookup('imagesStore'),
        tpl: imageTpl,
        itemSelector: 'div.thumb-wrap',
        emptyText: 'No images available'
    }
)
    
    function set_loadformprofile(){
        Ext.getCmp('panelprofile').getForm().load({
            url: '<?= site_url("profile/formload") ?>',
            success:function(form,action){
                //                var de = Ext.JSON.decode(action.response.responseText);               
                //                imageTpl.overwrite(getComp('imageview').body, selNode[0].data);

                //                getComp('imageview').refresh();
                //                getComp('profile_logo').setValue('<?php echo base_url(); ?>'+'assets/images/logomuh.jpg');
                //                readLog(de.data.url_logo);
            },
            failure: function(form, action){
                var de = Ext.JSON.decode(action.response.responseText);
                Ext.Msg.show({
                    title: 'Error',
                    msg: de.errMsg,
                    modal: true,
                    icon: Ext.Msg.ERROR,
                    buttons: Ext.Msg.OK,
                    fn: function(btn){
                        if (btn == 'ok' && de.errMsg == 'Session Expired') {
                            window.location = '<?= site_url("auth/login") ?>';
                        }
                    }
                });
            }
        });
    }
    defineTabFormDynamis(
    {
        name:'MyTabProfile',
        xtype:'TabProfile',
        alias:'widget.TabProfile',
        title:'Profile Sekolah',
        id:'tab21'
        ,items:[
            {
                xtype: 'form',
                autoShow: true,
                id: 'panelprofile',
                region: 'north',
                margins: '5 5 5 5',
                bodyPadding: '5 5 5 5',
                layout: 'column',     
                defaultType:'textfield',
                defaults: { 
                            
                    allowBlank: false,
                    labelWidth: 100,
                    labelAlign: 'left',
                    anchor:'40%'
                },
                items:[
                    {
                        xtype: 'form',
                        columnWidth:.5,                    
                        layout: 'form',
                        border:0,
                        bodyPadding: '5 5 5 5',
                        defaultType:'textfield',
                        defaults: { 
                            allowBlank: false,
                            labelWidth: 100,
                            labelAlign: 'left',
                            anchor:'40%'
                        },
                        items:[{
                                name: 'nama_sekolah',
                                id: 'profile_nama',
                                fieldLabel: 'Nama Sekolah',
                                maxLength:100,
                                emptyText: 'Nama Sekolah',
                                allowBlank: false
                                //                                ,readOnly:true                    
                            },
                            {
                                name: 'alamat_sekolah',
                                id: 'profile_alamat',
                                fieldLabel: 'Alamat Sekolah',
                                //                                maxLength:50,
                                emptyText: 'Alamat Sekolah',
                                allowBlank: false
                                //                                ,readOnly:true                    
                            }
                            ,{
                                name: 'id_nasabah_bpr',
                                id: 'profile_idnasabah',
                                fieldLabel: 'ID Nasabah BPR',
                                maxLength:20,
                                emptyText: 'ID Nasabah BPR',
                                allowBlank: false
                                //                                ,readOnly:true                    
                            }
                        ]
                        ,bbar:['->','-',
                            {
                                text: 'Simpan Profile Data',
                                //                        margins: '0 10 0 10',
                                iconCls: 'icon-simpan',
                                handler: function(){
                                    var form = this.up('form').getForm();
                                    if(form.isValid()){
                                        form.submit({
                                            url:  '<?php echo base_url(); ?>' + 'profile/update_rows',
                                            params:{
                                                cmd: 'data'},
                                            waitMsg: 'Updating Data ...',
                                            success: function(fp, o) {
                                                var resp=Ext.JSON.decode(o.response.responseText);
                                                //                                                set_message(0, resp.msg);
                                                Ext.Msg.show({
                                                    title:'Message Info',
                                                    msg: resp.msg,
                                                    buttons: Ext.Msg.OK,
                                                    icon: Ext.Msg.INFO,
                                                    fn:function(btn){
                                                        if (btn == 'ok'){
                                                            window.location='<?= base_url(); ?>';
                                                        }
                                                    }
                                                });
                                                //                                                        msg('Success', tpl.apply(o.result));
                                            },
                                            failure:function(fp, o) {
                                                var resp=Ext.JSON.decode(o.response.responseText);
                                                set_message(1, resp.msg);
                                            }
                                        });
                                    }
                                }
                            }
                        ]
                    },
                    {
                        xtype: 'form',
                        columnWidth:.4,                    
                        layout: 'fit',
                        bodyPadding: '5 5 5 5',
                        border:0,
                        defaultType:'textfield',
                        defaults: { 
                            //                            allowBlank: false,
                            labelWidth: 80,
                            labelAlign: 'left',
                            anchor:'50%'
                        },
                        items:[
                            ,{
                                xtype:'panel',
                                layout:'form',
                                //                        anchor:'30%',
                                title:'Upload Logo',
                                //                        height:50,
                                bodyPadding: '5 5 5 5',
                                defaults: { 
                            
                                    allowBlank: true,
                                    labelWidth: 95,
                                    labelAlign: 'left',
                                    anchor:'90%'
                                },
                                items:[
                                    {
                                        xtype: 'hidden',
                                        fieldLabel: 'Name',
                                        id:'profile_logo',
                                        name:'url_logo'
                                    },
                                   
                                    {
                                        xtype: 'filefield',
                                        id: 'form_file',
                                        emptyText: 'Select an image',
                                        fieldLabel: 'Upload Logo',
                                        name: 'photo-path',
                                        buttonText: 'Upload'
                                        ,allowBlank: false
                                        //                                        ,flex:1
                                    },{
                                        xtype:'imageviewfield',
                                        id:'image_viewf',
                                        name:'imgvf'
                                    }
//                                    ,Ext.create('Ext.view.View', {
//                                        fieldLabel: 'Logo',
//                                        id:'image_view',
//                                        name:'imgv',
//                                        store: imageStore, //Ext.data.StoreManager.lookup('imagesStore'),
//                                        tpl: imageTpl,
//                                        itemSelector: 'div.thumb-wrap',
//                                        emptyText: 'No images available'
//                                        //    renderTo: Ext.getBody()
//                                    })
                                ]
                      
                            }
                        ],bbar:['->',
                            '-',
                            {
                                text: 'Simpan Logo',
                                iconCls: 'icon-simpan',
                                handler: function(){
                                    var form = this.up('form').getForm();
                                    if(form.isValid()){
                                        form.submit({
                                            url:  '<?php echo base_url(); ?>' + 'profile/upload_file',
                                            waitMsg: 'Uploading your logos...',
                                            success: function(fp, o) {
                                                var resp=Ext.JSON.decode(o.response.responseText);
                                                Ext.Msg.show({
                                                    title:'Message Info',
                                                    msg: resp.msg,
                                                    buttons: Ext.Msg.OK,
                                                    icon: Ext.Msg.INFO,
                                                    fn:function(btn){
                                                        if (btn == 'ok'){
                                                            window.location='<?= base_url(); ?>';
                                                        }
                                                    }
                                                });
                                            },
                                            failure:function(fp, o) {
                                                var resp=Ext.JSON.decode(o.response.responseText);
                                                set_message(1, resp.msg);
                                            }
                                        });
                                    }
                                }
                            },'-'
                        ]
                    }
                
                    
                    

                ]
                
            }
        ]
    },function(){
        set_loadformprofile();
        imageStore.load();
    }
);
</script>